<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="scientificList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="封面图" align="center" prop="bannerPic">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bannerPic"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <div style="word-wrap: break-all; display: -webkit-box; -webkit-box-orient: vertical;-webkit-line-clamp: 2; overflow: hidden;" v-html="scope.row.content.replace(/<img[^>]*>|<video[^>]*>/g, '')"></div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enabled"  :active-value="1" :inactive-value="0"></el-switch>
        </template>
       
      </el-table-column> -->
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | timestampToDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, scope.$index)"
            v-hasPermi="['frist:scientific:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listScientific, getScientific, delScientific, addScientific, updateScientific } from "@/api/frist/scientific";
import { timestampToDate } from '@/utils/time';


export default {
  name: "Scientific",

  filters: {
    timestampToDate
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科学可视化表格数据
      scientificList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerPic: null,
        title: null,
        enabled: null,
        content: null,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询科学可视化列表 */
    getList() {
      this.loading = true;
      listScientific(this.queryParams).then(response => {
        this.scientificList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bannerPic: null,
        title: null,
        enabled: null,
        content: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加科学可视化";
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {


      sessionStorage.setItem("scientific", JSON.stringify(row));

      this.$router.push({
        path: `/frist/scientific/edit/${row.id}`,
      })

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除科学可视化编号为"' + ids + '"的数据项？').then(function () {
        return delScientific(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('frist/scientific/export', {
        ...this.queryParams,
        ids: this.ids.length ? this.ids : undefined
      }, `scientific_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
